package com.lx.composetest.ink

import android.util.Log
import com.google.mlkit.common.MlKitException
import com.google.mlkit.common.model.DownloadConditions
import com.google.mlkit.common.model.RemoteModelManager
import com.google.mlkit.vision.digitalink.DigitalInkRecognition
import com.google.mlkit.vision.digitalink.DigitalInkRecognitionModel
import com.google.mlkit.vision.digitalink.DigitalInkRecognitionModelIdentifier
import com.google.mlkit.vision.digitalink.DigitalInkRecognizer
import com.google.mlkit.vision.digitalink.DigitalInkRecognizerOptions
import com.google.mlkit.vision.digitalink.Ink
import com.google.mlkit.vision.digitalink.RecognitionResult

/**
 *  desc : TODO Fill the fucking desc
 *
 *
 *  @author LuXin
 *  @createTime 2024/7/30
 */
object InkAnalyse {

    fun analysis(ink: Ink) {
        // Specify the recognition model for a language
        var modelIdentifier: DigitalInkRecognitionModelIdentifier? = null
        try {
//            modelIdentifier = DigitalInkRecognitionModelIdentifier.fromLanguageTag("zh-Hani")
            modelIdentifier = DigitalInkRecognitionModelIdentifier.fromLanguageTag("zh-Hani")
        } catch (e: MlKitException) {
            // language tag failed to parse, handle error.
            Log.e("Ink", "analysis: No Model Found")
        }
        if (modelIdentifier == null) {
            // no model was found, handle error.
            Log.e("Ink", "analysis: No Model Found")
            return
        }
        var model: DigitalInkRecognitionModel =
            DigitalInkRecognitionModel.builder(modelIdentifier).build()
//
//        RemoteModelManager.getInstance().download(model, DownloadConditions.Builder().build())
//            .addOnSuccessListener {
//                Log.i("Ink", "Model downloaded")
//            }
//            .addOnFailureListener { e: Exception ->
//                Log.e("Ink", "Error while downloading a model: $e")
//            }

        // Get a recognizer for the language
        var recognizer: DigitalInkRecognizer =
            DigitalInkRecognition.getClient(
                DigitalInkRecognizerOptions.builder(model).build()
            )

        recognizer.recognize(ink)
            .addOnSuccessListener { result: RecognitionResult ->
                // `result` contains the recognizer's answers as a RecognitionResult.
                // Logs the text from the top candidate.
                result.candidates.forEach {
                    it.score
                }
                Log.i("Ink", "Success = " + result.candidates[0].text)
            }
            .addOnFailureListener { e: Exception ->
                Log.e("Ink", "Error during recognition: $e")
                e.printStackTrace()
            }
    }
}